﻿using Kenly.DBFramework.Gateway;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Collections.Generic;
using System;
using System.Runtime;

using Kenly.DBFramework.TestModel.Purchasing;
using Kenly.UTAssist;

namespace Kenly.DBFramework.Tests
{
    
    /// <summary>
    ///这是 ViewGatewayTest 的测试类，旨在
    ///包含所有 ViewGatewayTest 单元测试
    ///</summary>
    [TestClass()]
    public class ViewGatewayAsyncTest
    {
        private TestContext testContextInstance;

        /// <summary>
        ///获取或设置测试上下文，上下文提供
        ///有关当前测试运行及其功能的信息。
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region 附加测试属性
        // 
        //编写测试时，还可使用以下属性:
        //
        //使用 ClassInitialize 在运行类中的第一个测试前先运行代码
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //使用 ClassCleanup 在运行完类中的所有测试后再运行代码
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        
        //使用 TestInitialize 在运行每个测试前先运行代码
        [TestInitialize()]
        public void MyTestInitialize()
        {
        }
        
        //使用 TestCleanup 在运行完每个测试后运行代码
        [TestCleanup()]
        public void MyTestCleanup()
        {

        }
        
        #endregion


        [TestMethod()]
        public void ListAllAsyncTest()
        {
            Action testDelegate =new Action(ListAllVendor);

            AsyncTester asyncTester = new AsyncTester();

            int invokeTimes = 100;
            bool r = asyncTester.Invoke(testDelegate, invokeTimes,1000*5);

            Assert.IsTrue(r, "ListAllAsyncTest did not return expected value.");
        }

        private void ListAllVendor()
        {
            List<Vendor> actual = Vendor.ListAll();
            Assert.IsTrue(actual.Count > 0, "Vendor.ListAll did not return expected value.");
        }

        [TestMethod()]
        public void RetrieveByIdAsyncTest()
        {
            Action testDelegate = new Action(RetrieveById);

            AsyncTester asyncTester = new AsyncTester();

            int invokeTimes = 10;
            bool r = asyncTester.Invoke(testDelegate, invokeTimes,100);

            Assert.IsTrue(r, "RetrieveByIdAsyncTest did not return expected value.");
        }

        private void RetrieveById()
        {
            Vendor vendor = new Vendor();
            vendor.VendorId = 10;
            vendor.RetrieveById();
            Assert.IsNotNull(vendor.Name, "Vendor.RetrieveById did not return expected value.");
        }

        [TestMethod()]
        public void DistinctVendorNamesAsyncTest()
        {
            Action testDelegate = new Action(DistinctVendorNames);

            AsyncTester asyncTester = new AsyncTester();

            int invokeTimes = 50;
            bool r = asyncTester.Invoke(testDelegate, invokeTimes);

            Assert.IsTrue(r, "ViewGatewayAsyncTest did not return expected value.");
        }

        private void DistinctVendorNames()
        {
            List<string> actual = Vendor.DistinctVendorNames();

            actual.Sort();
            for (int i = 0; i < actual.Count - 1; i++)
            {
                Assert.IsTrue(actual[i] != actual[i + 1], "Vendor.DistinctVendorNames did not return expected value.");
            }
        }
        
    }
}
